package com.bdqn.rooms.controller;


import com.bdqn.rooms.beans.Ruser;
import com.bdqn.rooms.service.RuserService;
import com.bdqn.rooms.service.impl.RuserServiceImpl;
import com.bdqn.rooms.util.Result;
import com.bdqn.rooms.util.ResultEnum;
import com.bdqn.rooms.util.ResultUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jack
 * @since 2019-05-14
 */
@Controller
@RequestMapping("/ruser")
public class RuserController {
    @Resource
    private RuserService ruserServiceImpl;

    /**
     * 添加商户信息
     * @param ruser
     * @return
     */
    @RequestMapping("/addRuser")
    @ResponseBody
    public Result addRuser(Ruser ruser,
                           @RequestParam(value="Rimge",required = false) MultipartFile Rimge,
                           HttpServletRequest request){
        String image = Rimge.getOriginalFilename();
        String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
        File targetFile = new File(path,image);
        if(!targetFile.exists()){
            targetFile.mkdir();
        }
        try {
            Rimge.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.ERROR);
        }
        ruser.setRimage(image);
        ruserServiceImpl.insert(ruser);//返回主键id  作为参数跳转到其他页面

        return ResultUtil.success();
    }



}

